package com.gxa.modules.service.zxy.impl;

import com.gxa.common.vo.R2;
import com.gxa.modules.mapper.zxy.TCMapper;
import com.gxa.modules.pojo.Package;
import com.gxa.modules.pojo.UserOrders;
import com.gxa.modules.service.zxy.TCService;
import com.gxa.modules.utils.DateFormatUtils;
import com.gxa.modules.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Created with IntelliJ IDEA.
 * Author: zxy
 * Date: 2022/01/05 9:32
 * Description:
 * Version: V1.0
 */
@Service
public class TCServiceImpl implements TCService{
    @Autowired(required = false)
    private TCMapper mapper;
    @Override
    public R2 queryTCList() {
        String currentTime = DateUtils.getCurrentTime(DateFormatUtils.DATE_FORMAT1);
        List<Package> list = mapper.queryTCList();
        for (Package pac:list) {
            Long packageId = pac.getPackageId();
            Long total = mapper.queryTotal(packageId);
            pac.setShop(total);
            Long addShop = mapper.queryAdd(packageId,currentTime);
            pac.setAddShop(addShop);
        }
        R2 r = new R2();
        if(list!=null){
            r.setCode(0);
            r.setData(list);
            r.setMsg("success");
        }else{
            r.setMsg("false");
            r.setCode(500);
        }
        return r;
    }

    @Override
    public R2 queryOrder(Map map) {
        Set<String> set = map.keySet();
        int a = 0;
        for (String key:set) {
            if(key.equals("endDate")){
                a=1;
                break;
            }
        }
        Integer currentPage = 0;
        Integer limit = 5;
        for(String key : set){
            if(key.equals("limit")){
                currentPage = (Integer)map.get("currentPage");
                limit = (Integer)map.get("limit");
                currentPage = (currentPage-1)*limit;
            }
        }
        map.put("currentPage",currentPage);
        map.put("limit",limit);
        if(a==1){
            if(map.get("beginDate")!=null&&map.get("beginDate")!=""){
                String beginDate = (String)map.get("beginDate");
                String endDate = (String)map.get("endDate");
                beginDate = beginDate +" 00:00:00";
                endDate = endDate+" 00:00:00";
                map.put("beginDate",beginDate);
                map.put("endDate",endDate);
            }
        }
        List<UserOrders> list = mapper.queryOrder(map);
        Integer total = mapper.queryTotalOrder(map);
        R2 r = new R2();
        if(list!=null){
            r.setData(list);
            r.setCode(0);
            r.setMsg("success");
            r.setData2(total);
        }
        return r;
    }
}
